import 'package:flutter/material.dart';
import 'package:get/utils.dart';
import 'package:ljy_p_app/diffwidget/ljy_card_item.dart';
import 'package:ljy_p_app/inputwidget/ljy_input_widget.dart';
import 'package:ljy_p_app/localization/localization_key.dart';
import 'package:ljy_p_app/style/lj_style.dart';

/// issue 编辑输入框
class IssueEditDialog extends StatefulWidget {
  final String dialogTitle;

  final ValueChanged<String>? onTitleChanged;

  final ValueChanged<String> onContentChanged;

  final VoidCallback onPressed;

  final TextEditingController? titleController;

  final TextEditingController? valueController;

  final bool needTitle;

  final String? hintText;

  const IssueEditDialog(
    this.dialogTitle,
    this.onTitleChanged,
    this.onContentChanged,
    this.onPressed,
    { super.key,
      this.titleController,
      this.valueController,
      this.needTitle = true,
      this.hintText
    }
  );

  @override
  State<IssueEditDialog> createState() => _IssueEditDialogState();
}

class _IssueEditDialogState extends State<IssueEditDialog> {

  /// 标题输入框
  Widget _renderTitleInput() {
    return widget.needTitle ? Padding(padding: EdgeInsets.all(5.0),
    child: LjyInputWidget(
      hintText: LocalizationKey.issueEditIssueTitleTip.tr,
      onChanged: widget.onTitleChanged,
      controller: widget.titleController,
      obscureText: false,
    )) : Container();
  }

  ///快速输入框
  _renderFastInputContainer() {
    ///因为是Column下包含了ListView，所以需要设置高度
    return SizedBox(
      height: 30.0,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: FAST_INPUT_LIST.length,
        itemBuilder: (context, index) {
          return RawMaterialButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              padding: const EdgeInsets.only(
                  left: 8.0, right: 8.0, top: 5.0, bottom: 5.0),
              constraints: const BoxConstraints(minWidth: 0.0, minHeight: 0.0),
              child: Icon(FAST_INPUT_LIST[index].iconData, size: 16.0),
              onPressed: () {
                String text = FAST_INPUT_LIST[index].content;
                String newText = "";
                if (widget.valueController?.value != null) {
                  newText = widget.valueController!.value.text;
                }
                newText = newText + text;
                setState(() {
                  widget.valueController!.value =
                      TextEditingValue(text: newText);
                });
                widget.onContentChanged.call(newText);
              });
        }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: SafeArea(
        child: SingleChildScrollView(
          child: Container(
            height: MediaQuery.sizeOf(context).height,
            width: MediaQuery.sizeOf(context).width,
            color: Colors.black12,
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onTap: () {
                FocusScope.of(context).requestFocus(FocusNode());
              },
              child: Center(
                child: LjyCardItem(
                  margin: EdgeInsets.only(left: 50.0, right: 50.0),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0)
                  ),
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        /// 标题
                        Padding(padding: EdgeInsets.only(top: 5.0, bottom: 15.0),
                          child: Center(child: Text(widget.dialogTitle, style: LJConstant.normalTextBold))
                        ),
                        _renderTitleInput(),
                    
                        /// 内容输入框
                        Container(
                          height: MediaQuery.sizeOf(context).width * 3 / 4,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.all(Radius.circular(4.0)),
                            color: Colors.white,
                            border: Border.all(color: LJColors.subTextColor, width: 0.5)
                          ),
                          padding: EdgeInsets.only(left: 20.0, right: 20.0, top: 12.0, bottom: 12.0),
                          child: Column(
                            children: [
                              Expanded(
                                child: TextField(
                                  autofocus: false,
                                  maxLines: 999,
                                  onChanged: widget.onContentChanged,
                                  controller: widget.valueController,
                                  decoration: InputDecoration(
                                    hintText: widget.hintText ?? LocalizationKey.issueEditIssueTitleTip.tr,
                                    hintStyle: LJConstant.middleSubText,
                                    isDense: true,
                                    border: InputBorder.none
                                  ),
                                  style: LJConstant.middleText,
                                ),
                              ),

                              _renderFastInputContainer(),
                            ],
                          ),
                        ),
                        
                        SizedBox(height: 10,),
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            // 取消
                            Expanded(child: RawMaterialButton(onPressed: (){
                              Navigator.pop(context);
                            },
                            materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              padding: EdgeInsets.all(4.0),
                              constraints: BoxConstraints(minHeight: 0.0, minWidth: 0.0),
                              child: Text(LocalizationKey.appCancel.tr, style: LJConstant.normalSubText),
                            )),
                            
                            Container(width: .3, height: 25.0, color: LJColors.subTextColor,),

                            Expanded(child: RawMaterialButton(onPressed: widget.onPressed,
                              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                              padding: EdgeInsets.all(4.0),
                              constraints: BoxConstraints(minHeight: 0.0, minWidth: 0.0),
                              child: Text(LocalizationKey.appOk.tr, style: LJConstant.normalTextBold),
                            )),
                          ],
                        )
                      ],
                    ),
                  )
                ),
              ),
            ),
          ),
        )
      ),
    );
  }
}

var FAST_INPUT_LIST = [
  FastInputIconModel(LJICons.ISSUE_EDIT_H1, "\n# "),
  FastInputIconModel(LJICons.ISSUE_EDIT_H2, "\n## "),
  FastInputIconModel(LJICons.ISSUE_EDIT_H3, "\n### "),
  FastInputIconModel(LJICons.ISSUE_EDIT_BOLD, "****"),
  FastInputIconModel(LJICons.ISSUE_EDIT_ITALIC, "__"),
  FastInputIconModel(LJICons.ISSUE_EDIT_QUOTE, "` `"),
  FastInputIconModel(LJICons.ISSUE_EDIT_CODE, " \n``` \n\n``` \n"),
  FastInputIconModel(LJICons.ISSUE_EDIT_LINK, "[](url)"),
];

class FastInputIconModel {
  final IconData iconData;
  final String content;

  FastInputIconModel(this.iconData, this.content);
}
